﻿// CMyMenu.cpp: 实现文件
//

#include "pch.h"
#include "MyDrawMenu.h"
#include "CMyMenu.h"

// CMyMenu

IMPLEMENT_DYNAMIC(CMyMenu,CMenu)

CMyMenu::CMyMenu()
{
	m_nIndex = 0;
}

CMyMenu::~CMyMenu()
{

}

BOOL CMyMenu::ChangeMenuItem(CMenu* pMenu, BOOL bTop)
{
	if (pMenu != NULL)
	{
		int nCount = pMenu->GetMenuItemCount();
		for (int i=0;i< nCount;i++)
		{
			pMenu->GetMenuString(i, m_ItemList[m_nIndex].m_strItemText,MF_BYPOSITION);
			int nItemID = pMenu->GetMenuItemID(i);
			if (nItemID == -1 && bTop)
			{
				nItemID = -2;
			}

			m_ItemList[m_nIndex].m_nItemID = nItemID;

			m_nIndex += 1;

			pMenu->ModifyMenu(i,MF_BYPOSITION| MF_OWNERDRAW| MF_STRING, m_ItemList[m_nIndex].m_nItemID,(LPCTSTR)&(m_ItemList[m_nIndex]));

			CMenu* pSubMenu = pMenu->GetSubMenu(i);

			if (pSubMenu != NULL)
			{
				ChangeMenuItem(pSubMenu);
			}
		}
	}
	return TRUE;
}

BOOL CMyMenu::AttachMenu(UINT nID)
{
	this->LoadMenu(nID);
	return TRUE;
}

void CMyMenu::DrawItemText(CDC* pDC, LPSTR str, CRect rect)
{
	rect.DeflateRect(20,0);
	pDC->DrawText(str, rect, DT_SINGLELINE | DT_VCENTER | DT_LEFT);

}

void CMyMenu::DrawTopMenu(CDC* pDC, CRect rect, BOOL bSelected)
{
	if (bSelected)
	{
		pDC->SelectStockObject(BLACK_PEN);
		pDC->Rectangle(&rect);
		rect.DeflateRect(1,1);
		pDC->FillSolidRect(rect,RGB(150,185,255));
	}
	else
	{
		CRect rc;
		AfxGetMainWnd()->GetClientRect(&rc);
		rc.top = rect.top;
		rc.bottom = rect.bottom;
		rc.left = 350;
		rc.right += 4;
		pDC->FillSolidRect(&rc,RGB(192,192,192));
		pDC->FillSolidRect(&rect,RGB(192, 192, 192));
	}
}

void CMyMenu::DrawSeparater(CDC* pDC, CRect rect)
{
	if (pDC != NULL)
	{
		pDC->Draw3dRect(rect,RGB(255,0,0), RGB(0, 0, 255));
	}
}

// CMyMenu 消息处理程序

void CMyMenu::DrawComMenu(CDC* pDC, CRect rect, COLORREF colorFrom, COLORREF colorTo, BOOL bSelected)
{
	if (bSelected)
	{
		pDC->Rectangle(&rect);
		rect.DeflateRect(1,1);

		int r1, g1, b1;
		r1 = GetRValue(colorFrom);
		g1 = GetGValue(colorFrom);
		b1 = GetBValue(colorFrom);

		int r2, g2, b2;
		r2 = GetRValue(colorTo);
		g2 = GetGValue(colorTo);
		b2 = GetBValue(colorTo);

		float r3, g3, b3;

		r3 = (float)(r2 - r1) / (float)(rect.Height());
		g3 = (float)(g2 - g1) / (float)(rect.Height());
		b3 = (float)(b2 - b1) / (float)(rect.Height());

		COLORREF r, g, b;
		CPen* pOldPen = new CPen();

		for (int i = rect.top;i< rect.bottom;i++)
		{
			r = r1 + (int)(r3 * (i - rect.top));
			g = g1 + (int)(g3 * (i - rect.top));
			b = b1 + (int)(b3 * (i - rect.top));

			CPen pen(BS_SOLID,1,RGB(r,g,b));
			pDC->SelectObject(&pen);
			pDC->MoveTo(rect.left,i);
			pDC->LineTo(rect.right, i);
		}

		pDC->SelectObject(pOldPen);

	}
	else
	{
		pDC->FillSolidRect(rect,RGB(0x000000F9, 0x000000F8, 0x000000F7));
	}

}

void CMyMenu::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	if (lpDrawItemStruct->CtlType == ODT_MENU)
	{
		if (lpDrawItemStruct->itemData == NULL)
			return;
		unsigned int nState = lpDrawItemStruct->itemState;
		CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
		CString strText = ((MenuItemInfo*)lpDrawItemStruct->itemData)->m_strItemText;
		LPSTR str = strText.GetBuffer(strText.GetLength());

		int nItemID = ((MenuItemInfo*)lpDrawItemStruct->itemData)->m_nItemID;

		CRect rect = lpDrawItemStruct->rcItem;

		pDC->SetBkMode(TRANSPARENT);

		switch (nItemID)
		{
		case -2:
		{
			DrawTopMenu(pDC, rect, (nState& ODS_SELECTED)||(nState & 0x0040));
			DrawItemText(pDC, str, rect);
			break;
		}
		case -1:
		{
			DrawItemText(pDC, str, rect);
			break;
		}
		case 0:
		{
			DrawSeparater(pDC, rect);
			break;
		}
		default:
		{
			DrawComMenu(pDC, rect, 0xfaa0, 0xf00ff, nState & ODS_SELECTED);
			DrawItemText(pDC, str, rect);
			break;
		}
		}
	}
}


void CMyMenu::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
	// TODO:  添加您的代码以确定指定项的大小
	if (lpMeasureItemStruct->CtlType == ODT_MENU)
	{
		lpMeasureItemStruct->itemHeight = ITEMHEIGHT;
		lpMeasureItemStruct->itemWidth = ITEMWIDTH;

		MenuItemInfo* pMenuItemInfo = (MenuItemInfo*)lpMeasureItemStruct->itemData;


		int nLength = ((MenuItemInfo*)lpMeasureItemStruct->itemData)->m_strItemText.GetLength();

		lpMeasureItemStruct->itemWidth = nLength  * 10;
		switch (pMenuItemInfo->m_nItemID)
		{
		case 0:
			lpMeasureItemStruct->itemHeight = 1;
			break;
		}
	}
}
